package com.vcc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.ShareAccountOrder;
import com.vcc.data.mapper.ShareAccountOrderMapper;
import com.vcc.service.boss.share.order.response.BossShareAccountOrderResponse;
import com.vcc.service.client.share.order.response.ClientShareAccountOrderResponse;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.ShareAccountOrderService;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.ShareAccountOrderTableDef.SHARE_ACCOUNT_ORDER;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class ShareAccountOrderServiceImpl extends ServiceImpl<ShareAccountOrderMapper, ShareAccountOrder> implements ShareAccountOrderService {
    @Override
    public Boolean createShareAccountOrder(Long userId, String remark) {
        ShareAccountOrder shareAccountOrder = new ShareAccountOrder();
        shareAccountOrder.setUserId(userId);
        shareAccountOrder.setOrderId(IdUtil.getSnowflakeNextIdStr());
        shareAccountOrder.setRemark(remark);
        shareAccountOrder.setRequestDate(new Date());
        shareAccountOrder.setStatus(2);
        return save(shareAccountOrder);
    }

    @Override
    public Page<ClientShareAccountOrderResponse> paging(Integer pageNumber, Integer pageSize, Long userId, Integer status) {
        QueryChain<ShareAccountOrder> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT_ORDER.ALL_COLUMNS);
        queryChain.select(BOSS_USER.EMAIL.as("auditAccount"));
        queryChain.leftJoin(BOSS_USER).on(SHARE_ACCOUNT_ORDER.AUDIT_USER_ID.eq(BOSS_USER.ID));

        queryChain.where(SHARE_ACCOUNT_ORDER.USER_ID.eq(userId));
        if (status != null) {
            queryChain.where(SHARE_ACCOUNT_ORDER.STATUS.eq(status));
        }
        queryChain.orderBy(SHARE_ACCOUNT_ORDER.REQUEST_DATE.desc());
        return queryChain.pageAs(
                Page.of(
                        pageNumber == null ? 1 : pageNumber,
                        pageSize == null ? 20 : pageSize
                ),
                ClientShareAccountOrderResponse.class
        );
    }


    @Override
    public Page<BossShareAccountOrderResponse> paging(Integer pageNumber, Integer pageSize, String orderId, Long targetUserId, Integer status) {
        QueryChain<ShareAccountOrder> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT_ORDER.ALL_COLUMNS);
        queryChain.select(USER.EMAIL.as("account"));
        queryChain.select(BOSS_USER.as("dealer").EMAIL.as("dealerAccount"));
        queryChain.select(BOSS_USER.as("audit").EMAIL.as("auditAccount"));
        queryChain.leftJoin(USER).on(SHARE_ACCOUNT_ORDER.USER_ID.eq(USER.ID));
        queryChain.leftJoin(BOSS_USER.as("dealer")).on(BOSS_USER.ID.eq(USER.BE_LONG_TO_DEALER));
        queryChain.leftJoin(BOSS_USER.as("audit")).on(BOSS_USER.as("audit").ID.eq(SHARE_ACCOUNT_ORDER.AUDIT_USER_ID));
        if (!BossUserUtil.hasAllData()) {
            Long bossUserId = BossUserUtil.getId();
            queryChain.and(
                    BOSS_USER.as("dealer").ID.eq(bossUserId)
                            .or(BOSS_USER.as("dealer").PARENT_ID.eq(bossUserId))
            );
        }
        if (targetUserId != null) {
            queryChain.where(SHARE_ACCOUNT_ORDER.USER_ID.eq(targetUserId));
        }
        if (orderId != null && !orderId.isEmpty()) {
            queryChain.where(SHARE_ACCOUNT_ORDER.ORDER_ID.eq(orderId));
        }
        if (status != null) {
            queryChain.where(SHARE_ACCOUNT_ORDER.STATUS.eq(status));
        }
        queryChain.orderBy(SHARE_ACCOUNT_ORDER.REQUEST_DATE.desc());
        return queryChain.pageAs(
                Page.of(
                        pageNumber == null ? 1 : pageNumber,
                        pageSize == null ? 20 : pageSize
                ),
                BossShareAccountOrderResponse.class
        );
    }

    @Override
    public Boolean audit(Long id, Integer status, String remark) {
        UpdateChain<ShareAccountOrder> updatedChain = updateChain();
        updatedChain.set(ShareAccountOrder::getStatus, status);
        updatedChain.set(ShareAccountOrder::getAuditUserId, BossUserUtil.getId());
        updatedChain.set(ShareAccountOrder::getAuditDate, new Date());
        updatedChain.set(ShareAccountOrder::getRemark, remark);
        updatedChain.eq(ShareAccountOrder::getId, id);
        return updatedChain.update();
    }
}
